<template>
  <div class="paperManage">
      <div class="title">系统设置 / 文章管理</div>
      <div class="table_body">
        <template>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="论文管理" name="first">
              <el-button class="add" type="primary" size="small" @click="add('article')">新增</el-button>
              <el-button class="add" v-show="this.$route.query.teacherid != null" type="primary" size="small" @click="addArticle">添加到教师发表文章</el-button>
              <el-button class="add" v-show="this.$route.query.researchid != null" type="primary" size="small" @click="addResearchArticle">添加代表性文章</el-button>
              <div class="byTitle">
                标题名称：
                <el-input
                    class="byTitleInput"
                    placeholder="请输入内容"
                    prefix-icon="el-icon-search"
                    size="small"
                    clearable
                    @keyup.enter.native="getArticleDataList"
                    v-model="articleQueryList.title">
                </el-input>
              </div>
              <div class="byYear">
                按年份筛选：
                <el-date-picker
                    class="byYearInput"
                    v-model="articleQueryList.publishdate"
                    type="year"
                    value-format="yyyy"
                    @keyup.enter.native="getArticleDataList"
                    placeholder="请选择年份">
                </el-date-picker>
                <el-button class="search" type="primary" size="small" @click="getArticleDataList">搜索</el-button>
              </div>
              <div class="event">
                <tableArea
                        :label-map="articleLabelMap"
                        :table-data="articleTableData"
                        :total="articleTotal"
                        :query="articleQueryList"
                        :page="articleQueryList.page"
                        :limit="articleQueryList.pagesize"
                        :checkBox="isCheckBox"
                        @selectIds="getSelectIds"
                        @pagination="handleArticlePagination"
                        @edit="articleEdit"
                        @delete="articleDelete"
                ></tableArea>
              </div>
            </el-tab-pane>
            <el-tab-pane label="书籍管理" name="second">
              <el-button class="add" type="primary" size="small" @click="add('book')">新增</el-button>
              <div class="event">
                <tableArea
                        :label-map="bookLabelMap"
                        :table-data="bookTableData"
                        :total="bookTotal"
                        :query="bookQueryList"
                        :page="bookQueryList.page"
                        :limit="bookQueryList.pagesize"
                        @pagination="handleBookPagination"
                        @edit="bookEdit"
                        @delete="bookDelete"
                ></tableArea>
              </div>
            </el-tab-pane>
            <el-tab-pane label="专利管理" name="third">
              <el-button class="add" type="primary" size="small" @click="add('patent')">新增</el-button>
              <div class="event">
                <tableArea
                        :label-map="patentLabelMap"
                        :table-data="patentTableData"
                        :total="patentTotal"
                        :query="patentQueryList"
                        :page="patentQueryList.page"
                        :limit="patentQueryList.pagesize"
                        @pagination="handlePatentPagination"
                        @edit="patentEdit"
                        @delete="patentDelete"
                ></tableArea>
              </div>
            </el-tab-pane>
          </el-tabs>
        </template>
      </div>
    <el-dialog :title="articleType === 'add' ? '新增期刊论文' : '修改期刊论文信息'" :visible.sync="showAddThesis" @close="closeArticle" width="60%">
        <add-thesis ref="articleRef" @updateTable="updateTableArticle"  :articleInfo="addArticleDate" :operateType="articleType"></add-thesis>
    </el-dialog>
    <el-dialog :title="bookType === 'add' ? '新增书籍' : '修改书籍信息'" :visible.sync="showAddBook" @close="closeBook" width="60%">
      <add-book ref="bookRef" @updateTable="updateTableBook" :bookInfo="addBookDate" :operateType="bookType"></add-book>
    </el-dialog>
    <el-dialog :title="patentType === 'add' ? '新增专利' : '修改专利信息'" :visible.sync="showAddPatent" @close="closePatent" width="60%">
      <add-patent ref="patentRef" @updateTable="updateTablePatent" :patentInfo="addPatentDate" :operateType="patentType"></add-patent>
    </el-dialog>
  </div>
</template>

<script>
  import tableArea from '../components/tableArea'
  import AddThesis from "./thesisManage/addThesis/index";
  import AddBook from "./bookManage/addBook/index";
  import AddPatent from './patentManage/addPatent/index';
  import { getArticleList,deleteArticle,getBookList,deleteBook,getPatentList,deletePatent } from "@/api/paperManage";
  import { addTeacherArticle } from '@/api/memberManage'
  import { addResearchArticle } from '@/api/eventManage'
  export default {
    data() {
      return {
        titleInput: '',
        selectedIds: [],
        isCheckBox: false,
        activeName: 'first',
        articleType: 'add',
        bookType: 'add',
        patentType: 'add',
        showAddThesis: false,
        showAddBook: false,
        showAddPatent: false,
        addArticleDate: {},
        addBookDate: {},
        addPatentDate: {},
        articleLabelMap: [
          { label: '创建时间', prop: 'createdate' },
          { label: '作者', prop: 'author' },
          { label: '刊物', prop: 'journalname' },
          { label: '题目', prop: 'title' },
          { label: '是否可见', prop: 'flag' },
          { label: '编辑/删除', prop: 'operation' }
        ],
        articleTableData: [],
        articleTotal: 10,
        articleQueryList: {
          page: 1,
          pagesize: 10,
          title: '',
          publishdate: '',
        },
        bookLabelMap: [
          { label: '创建时间', prop: 'createdate' },
          { label: '作者', prop: 'author' },
          { label: '书名', prop: 'bookname' },
          {label: '照片', prop: 'picture'},
          { label: '是否可见', prop: 'flag' },
          { label: '编辑/删除', prop: 'operation' }
        ],
        bookTableData: [],
        bookTotal: 10,
        bookQueryList: {
          page: 1,
          pagesize: 10,
          bookname: '',
          publishdate: ''
        },
        patentLabelMap: [
          {label:'创建时间',prop:'createdate'},
          { label: '标题', prop: 'title' },
          { label: '专利号', prop: 'number' },
          { label: '专利权人', prop: 'assignee' },
          {label: '发明人', prop: 'inventor'},
          { label: '是否可见', prop: 'flag' },
          { label: '编辑/删除', prop: 'operation' }
        ],
        patentTableData: [],
        patentTotal: 10,
        patentQueryList: {
          page: 1,
          pagesize: 10,
          year: '',
        },
      }
    },
    created() {
    },
    mounted(){
      if(this.$route.query.teacherid){
        this.isCheckBox  = true
      }
      if(this.$route.query.researchid){
        this.isCheckBox  = true
      }
      this.getArticleDataList();
      this.getBookDataList();
      this.getPatentDataList();
    },
    methods: {
      getSelectIds(val){
        this.selectedIds = val
      },
      updateTableArticle(){
        this.showAddThesis = false
        this.getArticleDataList()
      },
      updateTableBook(){
        this.showAddBook = false
        this.getBookDataList()
      },
      updateTablePatent(){
        this.showAddPatent = false
        this.getPatentDataList()
      },

      closeArticle(){
        this.$refs.articleRef.articleForm = {
          articleid: '',
          title: '',
          author: '',
          journalname: '',
          publishdate: '',
          doi: '',
          summary : '',
          picture: '',
          flag: '',
        }
      },
      closeBook(){
        this.$refs.bookRef.bookForm = {
          bookid: '',
          bookname: '',
          author: '',
          picture: '',
          publisher: '',
          publishdate: '',
          introduction: '',
          flag: '',
        }
      },
      closePatent(){
        this.$refs.patentRef.patentForm = {
          patentid: '',
          title: '',
          number: '',
          assignee: '',
          inventor: '',
          flag: '',
        }
      },
      handleClick() {
       /* console.log(tab, event);*/
      },
      add(type) {
        switch (type) {
          case 'article':
            this.articleType = 'add'
            this.showAddThesis = true
            break;
          case 'book':
            this.bookType = 'add'
            this.showAddBook = true
            break;
          case 'patent':
            this.patentType = 'add'
            this.showAddPatent = true
            break;
        }
      },
      async getArticleDataList(){
        const res = await getArticleList(this.articleQueryList)
        console.log(res)
        this.articleTableData = res.rows.map((item)=>{
          item.operation =  ['编辑', '删除']
          return item
        })
        this.articleTotal = res.total
      },
      async getBookDataList(){
        const res = await getBookList(this.bookQueryList)
        console.log(res)
        this.bookTableData = res.rows.map((item)=>{
          item.operation = ['编辑', '删除']
          return item
        })
        this.bookTotal = res.total
      },
      async getPatentDataList(){
        const res = await getPatentList(this.patentQueryList)
        console.log(res)
        this.patentTableData = res.rows.map((item)=>{
          item.operation = ['编辑','删除']
          return item
        })
        this.patentTotal = res.total
      },

      handleArticlePagination(pages){
        this.articleQueryList.page = pages.page
        this.getArticleDataList()
      },
      handleBookPagination(pages){
        this.bookQueryList.page = pages.page
        this.getBookDataList()
      },
      handlePatentPagination(pages){
        this.patentQueryList.page = pages.page
        this.getPatentDataList()
      },

      articleEdit(row){
        this.articleType = 'edit'
        this.addArticleDate = {
          articleid: row.articleid,
          title: row.title,
          author: row.author,
          journalname: row.journalname,
          publishdate: row.publishdate,
          doi: row.doi,
          summary: row.summary,
          picture: row.picture,
          flag: row.flag,
        }
        this.showAddThesis = true
        console.log(row)
      },
      bookEdit(row){
        this.bookType = 'edit'
        this.addBookDate = {
          bookid: row.bookid,
          bookname: row.bookname,
          author: row.author,
          picture: row.picture,
          publisher: row.publisher,
          publishdate: row.publishdate,
          introduction: row.introduction,
          flag: row.flag,
        }
        this.showAddBook = true
        console.log(row)
      },
      patentEdit(row){
        this.patentType = 'edit'
        this.addPatentDate = {
          patentid: row.patentid,
          title: row.title,
          number:row.number,
          assignee: row.assignee,
          inventor: row.inventor,
          createdate: row.createdate,
          year: row.year,
          flag: row.flag,
        }
        this.showAddPatent = true
        console.log(row)
      },

      async handleArticleDelete(data){
        await deleteArticle({articleid: data.articleid})
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.getArticleDataList()
      },
      articleDelete(row){
        const _this = this
        this.$alert('确定删除吗', '警告', {
          confirmButtonText: '确定',
          callback: (action) => {
            console.log(action)
            if(action === 'confirm'){
              _this.handleArticleDelete(row)
            }
            this.$emit('updateTable')
          }
        });
      },
      async handleBookDelete(data){
        await deleteBook({bookid: data.bookid})
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.getBookDataList()
      },
      bookDelete(row){
        const _this = this
        this.$alert('确定删除吗', '警告', {
          confirmButtonText: '确定',
          callback: (action) => {
            console.log(action)
            if(action === 'confirm'){
              _this.handleBookDelete(row)
            }
            this.$emit('updateTable')
          }
        });
      },
      async handlePatentDelete(data){
        await deletePatent({patentid: data.patentid})
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.getPatentDataList()
      },
      patentDelete(row){
        const _this = this
        this.$alert('确定删除吗', '警告', {
          confirmButtonText: '确定',
          callback: (action) => {
            console.log(action)
            if(action === 'confirm'){
              _this.handlePatentDelete(row)
            }
          }
        });
      },

      async addArticle(){
        try {
          const res = await addTeacherArticle({
            teacherid: this.$route.query.teacherid,
            articleList: this.selectedIds
          })
          console.log(res)
          this.$message({
            type: 'success',
            message: '成功'
          })
          this.$router.push('MemberManage')
        } catch (e) {
          console.log(new Error(e))
        }
      },
      async addResearchArticle(){
        try {
          const res = await addResearchArticle({
            researchid: this.$route.query.researchid,
            articleList: this.selectedIds
          })
          console.log(res)
          this.$message({
            type: 'success',
            message: '成功'
          })
          this.$router.push('/SystemManage')
        } catch (e) {
          console.log(new Error(e))
        }
      }
    },
    computed: {},
    components: {tableArea, AddBook, AddThesis, AddPatent}
  }
</script>

<style lang="scss" scoped>
  .paperManage{
    width: 100%;
    .title{
      background:rgb(235,235,235);
      height: 50px;
      padding-left: 38px;
      padding-top: 22px;
      font-size: 18px;
      color: rgb(127,127,127);
    }
    .table_body{
      min-width: 100%;
      margin-top: 20px;
      padding-left: 38px;
      /deep/ .el-table th, .el-table tr {
        background:rgb(235,235,235);
      }
      /deep/ .el-tabs__item {
        padding: 0 40px;
        font-size: 16px;
      }
      /deep/ .el-button--medium {
        font-size: 16px;
        color: #D0021B;
        background: #cccccc;
        margin-left: 20px;
        margin-bottom: 10px;
      }
      /deep/ .el-table--enable-row-transition .el-table__body td {
        height: 58px;
      }
      /deep/ .el-table th>.cell {
        text-align: center;
      }
      /deep/ .el-table .cell {
        text-align: center;
        font-size: 16px;
      }
      /deep/ .el-pagination {
        padding: 22px 5px;
        text-align: center;
      }
      .byTitle{
        font-size: 16px;
        .byTitleInput{
          width: 601px;
          /deep/ .el-input__inner{
            width: 600px;
          }
        }
      }
      .byYear{
        font-size: 16px;
        margin-top: 15px;
        .byYeInput{
          width: 601px;
          /deep/ .el-input__inner{
            width: 600px;
          }
        }
      }
    }


  }
</style>
